package com.solocredit.finance.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.solocredit.finance.R
import com.solocredit.finance.app.MyApp
import com.solocredit.finance.base.utils.DensityUtils
import com.solocredit.finance.databinding.DialogMullaNetworkErrorBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.utils.event.MullaEventType

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/8 15:50
 * @Annotation：网络错误提示dialog
 */
class NetworkErrorDialog: ProDialog {

    private lateinit var binding: DialogMullaNetworkErrorBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initWindow() {
        val params = window?.attributes
        params?.width = MyApp.app.getPhoneWidth() - DensityUtils.dp2px(64f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.picker_view_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogMullaNetworkErrorBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.btnRetry)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v?.id == R.id.btnRetry){
            HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_NETWORK_ERROR_RELOAD,eventRemark)
            mCallback?.onMullaRetryClick()
        }
    }

    /**
     * 设置提示语
     */
    fun setTips(tips: String){
        binding.tvTips.text = tips
    }

    interface IDialogCallback {
        //点击重试
        fun onMullaRetryClick()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addMullaEventAction(MullaEventType.DIALOG.MULLA_DIALOG_NETWORK_ERROR_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addMullaEventAction(MullaEventType.DIALOG.MULLA_DIALOG_NETWORK_ERROR_DISMISS,eventRemark)
    }
}